package com.example.wac.controller;

import com.example.wac.annotation.AvoidResubmit;
import com.example.wac.annotation.RequestBodyAddParam;
import com.example.wac.annotation.UnJwtToken;
import com.example.wac.entity.Email;
import com.example.wac.mail.ToEmail;
import com.example.wac.service.EmailService;
import com.example.wac.service.MailService;
import com.example.wac.vo.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author wuancheng
 */
@Slf4j
@RestController
@RequestMapping("/email")
public class EmailController {
    @Autowired
    MailService mailService;

    @Resource(name = "emailService")
    EmailService emailService;

    @PostMapping("/text/rocketMq")
    public String rocket(@RequestBody ToEmail email) throws Exception {
       return mailService.rocketSendMail(email);
    }

    /**
     * {
     *     "tos":"984964551@qq.com",
     *     "subject":"发布订阅",
     *     "content":"测试测试时所所所",
     *     "from":"wacnihao@163.com"
     * }
     *
     * @param email
     * @return
     */
    @UnJwtToken
    @PostMapping("/rocket/rocketListen")
    public ApiResult rocketListen(@RequestBody ToEmail email){
        log.info("EmailController记录业务Id");
        return mailService.rocketListenSendMailCount(email);
    }

    /**
     *
     * @return
     */
    @UnJwtToken
    @GetMapping("/list")
    @AvoidResubmit
    public ApiResult list(){
        return emailService.findEmails();
    }

    /**
     *
     *
     * @return
     */
    @UnJwtToken
    @PostMapping("/query_list")
    @RequestBodyAddParam
    public ApiResult queryList(@RequestBody Map<String, Object> map){
        return emailService.findEmailsByCondition(map);
    }

    @PostMapping("/save")
    public ApiResult save(@RequestBody Email email){
       return emailService.saveEmail(email);
    }

    @PutMapping("/update")
    public ApiResult update(@RequestBody Email email){
       return emailService.update(email);
    }

    @DeleteMapping("/delete")
    public ApiResult delete(@RequestParam("id") Integer id){
       return emailService.delete(id);
    }
}